package com.lzj.myvideo.controller;

import com.lzj.myvideo.common.MD5Util;
import com.lzj.myvideo.common.RedisOperator;
import com.lzj.myvideo.common.ServerResponse;
import com.lzj.myvideo.service.UserService;
import com.shortvideo.pojo.User;
import com.shortvideo.vo.userVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class RegistAndLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    public RedisOperator redis;

    public static final String USER_REDIS_SESSION = "user_redis_session";

    /**
     * 将userToken加入user属性中，并且放入redis做无状态session
     * @param user
     * @return
     */
    public userVo setRedisToken(User user){
        userVo uservo = new userVo();
        String uniqueToken = UUID.randomUUID().toString().replace("-","");
        redis.set(USER_REDIS_SESSION+":"+user.getId(),uniqueToken,1000*60*30);
        BeanUtils.copyProperties(user,uservo);
        uservo.setUserToken(uniqueToken);
        return uservo;
    }

    /**
     * 注册接口
     * @param user
     * @return
     */
    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public ServerResponse regist(User user){
        if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())){
            return ServerResponse.createByError("用户名或密码不能为空");
        }else if (userService.checkUsernameIsExit(user.getUsername())){
            return ServerResponse.createByError("用户名已存在");
        }else {
            user.setId(UUID.randomUUID().toString().replace("-", ""));
            user.setNickname(user.getUsername());
            user.setPassword(MD5Util.encryptPassword(user.getPassword()));
            user.setFans_counts(0);
            user.setFollow_counts(0);
            user.setStars_counts(0);
            int resultCount = userService.saveUser(user);
            user.setPassword("");
            userVo userVo = this.setRedisToken(user);
            System.out.println(userVo.toString());
            if (resultCount > 0) {
                return ServerResponse.createBySuccess(userVo);
            } else {
                return ServerResponse.createByError("保存数据异常");
            }
        }
    }


    /**
     * 登陆接口
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ServerResponse login(User user) {
        System.out.println(user.toString());
        if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())){
            return ServerResponse.createByError("用户名或密码不能为空");
        }else {
            User userResult = userService.findUser(user);
            if (userResult != null){
                userResult.setPassword("");
                userVo userVo = this.setRedisToken(userResult);
                return ServerResponse.createBySuccess("登陆成功",userVo);
            }else
                return ServerResponse.createByError("登陆失败");
        }
    }

    /**
     * 注销接口
     * @param userid
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ServerResponse logout(String userid) {
        redis.del(USER_REDIS_SESSION+":"+userid);
        return ServerResponse.createBySuccess("注销成功");
        }
}
